package com.htj.workbench.service.impl;

import com.htj.setting.dao.UserDao;
import com.htj.setting.domain.User;
import com.htj.workbench.dao.ActivityDao;
import com.htj.workbench.dao.ActivityRemarkDao;
import com.htj.workbench.domain.Activity;
import com.htj.workbench.domain.ActivityRemark;
import com.htj.workbench.service.ActivityServiceInf;
import com.htj.workbench.vo.PageListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ActivityServiceInfImpl implements ActivityServiceInf {
    @Autowired
    ActivityDao activityDao;
    @Autowired
    ActivityRemarkDao activityRemarkDao;
    @Autowired
    UserDao userDao;
    @Override
    public boolean Save(Activity activity) {
        int count = activityDao.Save(activity);
        boolean bool = false;
        if(count == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public PageListVo<Activity> pageList(Map<String, Object> map) {
        PageListVo<Activity> pageListVo = new PageListVo<>();
        /*
        * dao需要查询total以及dataList
        * */
        int total = activityDao.getTotalByCondition(map);
        List<Activity> list = activityDao.getActivityByCondition(map);

        pageListVo.setList(list);
        pageListVo.setTotal(total);

        return pageListVo;
    }

    @Override
    @Transactional
    public boolean delete(String[] dis) {
        boolean bool = true;
        //查询删除备注数量
        int count1 = activityRemarkDao.getCountByAids(dis);
        //实际删除备注数量
        int count2 = activityRemarkDao.deleteByAids(dis);
        if(count2 != count1){
            bool = false;
        }
        //删除活动
        int count3 = activityDao.delete(dis);
        if(count3 != dis.length){
            bool = false;
        }
        return bool;
    }

    @Override
    public Map<String, Object> getUserListAndActivity(String id) {
        //1获得uList
        List<User> uList = userDao.getUserList();
        //2.获取a
        Activity a = activityDao.getActivityById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("uList",uList);
        map.put("a",a);
        return map;
    }

    @Override
    public boolean update(Activity activity) {
        boolean bool = false;
        int count = activityDao.update(activity);
        if(count == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public Activity detail(String id) {
        Activity a = activityRemarkDao.detail(id);
        return a;
    }

    @Override
    public List<ActivityRemark> getRemarkList(String aid) {
        List<ActivityRemark> list = activityRemarkDao.getRemarkList(aid);
        return list;
    }

    @Override
    public boolean deleteRemark(String id) {
        boolean bool = false;
        int count = activityRemarkDao.deleteRemark(id);
        if(count == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public boolean saveRemark(ActivityRemark ar) {
        boolean bool = false;
        int count = activityRemarkDao.saveRemark(ar);
        if(count == 1){
            bool = true;
        }
        return bool;
    }

    @Override
    public boolean editRemark(ActivityRemark ar) {
        boolean bool = false;
        int count = activityRemarkDao.editRemark(ar);
        if(count == 1){
            bool = true;
        }
        return bool;
    }

}
